
import matplotlib.pyplot as plt
import re



def parse_result(path):
	f=open(path)
	lines = f.readlines()
	f.close()
	ts = [[] for i in range(4)]
	for i in range(0,len(lines)):
		line = lines[i]
		mat = re.match("update_count=(\d+?), exec_count=(\d+?), time=(.+?), merge_num=(\d+)",line)
		if mat:
			for i in range(4):
				ts[i].append(float(mat.groups()[i]))
	print(ts)
	return ts


def parse_data(lambda_list,prefix):
	data = []
	for parm in lambda_list:
		ts = parse_result('result/test1_result/Geant-2012-10host-lambda-%d%s.txt'%(parm,prefix))
		data.append(ts)
	return data


lambda_list = (10,40)

data1 = parse_data(lambda_list,'')
data2 = parse_data(lambda_list,'-merge')

for i,_lambda in enumerate(lambda_list):
	plt.clf()
	data = data1
	uc_list = data[i][0]
	ex_list = data[i][1]
	plt.plot(uc_list,ex_list,'r',label='series')
	data = data2
	uc_list = data[i][0]
	ex_list = data[i][1]
	plt.plot(uc_list,ex_list,'b',label='simple merge')
	plt.title("$\lambda$=%d"%_lambda)
# 	plt.xticks(lambda_list)
	plt.xlabel("update number")
	plt.ylabel("operation number")
	plt.legend()
	plt.savefig('figs/test1_updatenum_opnum_lambda_%d.png'%_lambda)
	# plt.show()

def average(d):
	return float(sum(d))/len(d)

print(data1[0][0])
print(data2[0][0])
print(average(data2[0][3]))
print(average(data2[1][3]))
